package com.soap.desensitize.factory.encry.rreversible.asymmetric;

import com.soap.desensitize.encry.rreversible.asymmetric.AbstractAsymmetryEncry;
import com.soap.desensitize.exception.factory.StrategyFactoryException;
import com.soap.desensitize.factory.encry.EStrategy;
import com.soap.desensitize.strategy.rreversible.asymmetric.DsaStrategy;
import com.soap.desensitize.strategy.rreversible.asymmetric.RsaStrategy;

/**
 * @desc 可逆对称加密策略的工厂
 */
public class AsymmetricFactory {
    /**
     * @desc 根据Class 生产策略
     * @param cla
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static AbstractAsymmetryEncry create(Class<? extends AbstractAsymmetryEncry> cla) throws InstantiationException, IllegalAccessException {
        return cla.newInstance();
    }

    /**
     * @desc 根据Class 生产策略
     * @param cla
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static AbstractAsymmetryEncry create(Class<? extends AbstractAsymmetryEncry> cla,String pk,String sk) throws InstantiationException, IllegalAccessException {
        AbstractAsymmetryEncry strategy = cla.newInstance();
        strategy.setPk(pk);
        strategy.setSk(sk);
        return strategy;
    }

    /**
     * @desc 根据类路径生产策略
     * @param classStr
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractAsymmetryEncry create(String classStr) throws InstantiationException, IllegalAccessException, StrategyFactoryException {
        switch (classStr){
            case "com.rj.bd.strategy.rreversible.asymmetric.RsaStrategy":
                return RsaStrategy.getInstance();
            case "com.rj.bd.strategy.rreversible.asymmetric.DsaStrategy":
                return DsaStrategy.getInstance();
        }
        throw new StrategyFactoryException("没有找到这个策略 "+classStr);
    }

    /**
     * @desc 根据类路径生产策略
     * @param eStrategy 策略类枚举
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static AbstractAsymmetryEncry create(EStrategy eStrategy) throws InstantiationException, IllegalAccessException, StrategyFactoryException {
        return create(eStrategy.getClassName());
    }

    /**
     * @desc 根据类路径生产策略
     * @param eStrategy 策略类枚举
     * @param pk 公钥
     * @param sk 私钥
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractAsymmetryEncry create(EStrategy eStrategy,String pk,String sk) throws InstantiationException, IllegalAccessException, StrategyFactoryException {
        AbstractAsymmetryEncry strategy = create(eStrategy.getClassName());
        strategy.setPk(pk);
        strategy.setSk(sk);
        return strategy;
    }
}
